// packageB/pages/pay/pay.js
let app = getApp();

var applet_park_api_url = app.globalData.applet_park_api_url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carInfo: '',
    stopAddress: '',
    carNum: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.setData({
      carInfo: JSON.parse(options.carInfo),
      stopAddress: options.stopAddress,
      carNum: options.carNum,
    })
  },
  // 支付
  pay() {
    let that = this;
    var openId = app.globalData.openId || wx.getStorageSync("OPENID");
    var TOKEN = app.globalData.TOKEN || wx.getStorageSync("TOKEN");
    var nickName = wx.getStorageSync("nickName");
    wx.request({
      url: applet_park_api_url + '/api/unifiedOrder/createOrder',
      method: 'POST',
      header: {
        Authorization: TOKEN
      },
      data: {
        orderType: '04',
        totalMoney: that.data.carInfo.totalMoney, //that.data.price
        //         totalMoney: 1, //that.data.price
        carTime: that.data.carInfo.totalTime,
        realityMoney: that.data.carInfo.totalMoney, //
        //         realityMoney: 1, //
        actId: that.data.carInfo.id,
        openId: openId,
        nickName: nickName,
        address: that.data.stopAddress,
        carNum: that.data.carNum
      },
      success(res2) {
        
        if (res2.data.code == 0) {
          wx.requestPayment({
            timeStamp: res2.data.data.timeStamp,
            nonceStr: res2.data.data.nonceStr,
            package: res2.data.data.prepay_id,
            signType: 'MD5',
            paySign: res2.data.data.sign,
            paySource: 1,
            success(res) {
              
              wx.showToast({
                title: '支付成功',
              })

              setTimeout(function () {
                wx.showToast({
                  title: '页面跳转中',
                })
                wx.navigateTo({
                  url: '../stopCar/stopCar'
                })
              }, 500);
            },
            fail(res3) {
              
            }
          })

        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('goBack')) {
      wx.navigateBack()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var pages = this.getCurrentPages();
    if (pages[1].route === 'pages/home') {
         wx.navigateBack({delta: 1});
         //wx.reLaunch({ url: './welcome' });
     } else {
         
         wx.setStorageSync('goBack', true);
         wx.navigateBack({delta: 3});
         //wx.reLaunch({ url: './welcome' });
     }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})